// pages/cart/index.js
import {address,modal} from "../../public/js/request1"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //存储地址数据
    address:{},
    //存储购物车的商品数据
    cart_goods:[],
    //是否全选
    is_all:false,
    //总数量
    all_num:0,
    //总价
    all_price:0
  },

  //添加地址
  add_address(){
    wx.chooseAddress({
      success: (result) => {
        console.log(result);
        this.setData({
          address:result,
          ['address.addressall']:address(result)
        })
      }
    })
  },

  //结算按钮
  toplay(){
    let arry = this.data.cart_goods.filter(item => item.is_promote)
    if(!this.data.address.userName){
      wx.showToast({
        title: '请填写地址',
        icon:"error"
      })
      return
    }
    
    if(arry.length == 0){
      wx.showToast({
        title: '请添加商品',
        icon:"error"
      })
      return
    }

    wx.navigateTo({
      url: '../payment/payment'
    })
  },

  //改变是否选中
  is_ture(e){
    console.log(e);
    const id = e.currentTarget.dataset.id
    console.log(id);
    let c_index = this.data.cart_goods.findIndex(item => item.goods_id == id)
    let cart_goods =  this.data.cart_goods
    cart_goods[c_index].is_promote = !cart_goods[c_index].is_promote
    this.setData({
      cart_goods
    })
    wx.setStorageSync('cart',cart_goods)
    this.get_all()
  },

  //全部判断是否选中
  isall(e){
    console.log(e.detail.value[0]);
    // console.log(e.detail.value);
    let cart_goods = this.data.cart_goods
    cart_goods.forEach(item => item.is_promote = e.detail.value[0]?true:false)
    this.setData({
      cart_goods
    })

    wx.setStorageSync('cart', cart_goods)
    this.get_all()
  },

  //增加或者减少
  async add_and(e){
    let {num,id} = e.currentTarget.dataset
    let cart_goods = this.data.cart_goods
    let index = cart_goods.findIndex(item => item.goods_id == id)
    
    if(cart_goods[index].count == 1 && e.currentTarget.dataset.num == -1){
      const res = await modal({title:'确定删除该商品吗'})
      if (res.confirm) {
        cart_goods.splice(index,1)
      } else if (res.cancel) {
        cart_goods[index].count += 1
        console.log('用户点击取消')
      }
    }
    try{
      cart_goods[index].count += num
    }catch (err){
      console.log(err);
    }

    this.setData({
      cart_goods
    })
    wx.setStorageSync('cart', cart_goods)
    this.get_all()
  },

  //计算是否全选和总价和总数量
  get_all(id){
    let is_all = this.data.cart_goods[0].goods_id?this.data.cart_goods.every(item => item.is_promote):false
    let all_price = this.data.cart_goods.filter(item => item.is_promote).reduce((num,item) => {
      return num += item.goods_price * item.count
    },0)
    let all_num = this.data.cart_goods.filter(item => item.is_promote).reduce((num,item) => {
      return num += item.count
    },0)
    this.setData({
      is_all,
      all_price,
      all_num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
    
  onLoad: function (options) {
    // let cart_goods = wx.getStorageSync('cart') || []

    // this.setData({
    //   cart_goods
    // })

    // this.get_all()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let cart_goods = wx.getStorageSync('cart') || []

    this.setData({
      cart_goods
    })

    this.get_all()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})